package com.hnucm.cooklife.base.utils;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;

public class LoginUtils {
    //获取当前登录用户id
    public static int getLoginId(){
        long expire = SPUtils.getInstance("cookie").getLong("cookie_expire", 0);
        if (expire > System.currentTimeMillis()) {
            String cookies = SPUtils.getInstance("cookie").getString("user");
            if (!TextUtils.isEmpty(cookies)) {
                for (String cookie : cookies.split(";")) {
                    if (TextUtils.equals("userId", cookie.split("=")[0])) {
                        if (!TextUtils.isEmpty(cookie.split("=")[1])) {
                            return Integer.parseInt(cookie.split("=")[1]);
                        }
                        break;
                    }
                }
            }
        }
        return 0;
    }
    //获取当前登录用户信息
    public static String getLoginUser(){
        long expire = SPUtils.getInstance("cookie").getLong("cookie_expire", 0);
        if (expire > System.currentTimeMillis()) {
            String cookies = SPUtils.getInstance("cookie").getString("user");
            if (!TextUtils.isEmpty(cookies)) {
                for (String cookie : cookies.split(";")) {
                    if (TextUtils.equals("loginUserName", cookie.split("=")[0])) {
                        if (!TextUtils.isEmpty(cookie.split("=")[1])) {
                            return cookie.split("=")[1];
                        }
                        break;
                    }
                }
            }
        }
        return "";
    }

    //清空登录信息
    public static void clearLoginInfo() {
        SPUtils.getInstance("cookie").put("user", "");
    }

    //是否已经登录
    public static boolean isLogin() {
        return !TextUtils.isEmpty(getLoginUser());
    }

}
